/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import jpa.exceptions.NonexistentEntityException;
import model.Comment;
import model.CommentQuery;
import model.Event;
import model.EventQuery;
import model.HistoryPayment;
import model.HistoryPaymentQuery;
import model.User;

/**
 *
 * @author Kelompok 3
 */
public class EventManagementLogic {

    HttpServletRequest request;

    public EventManagementLogic(HttpServletRequest request) {
        this.request = request;
    }

    /* method untuk melakukan fungsi tambahEvent, yaitu mengambil nilai textfield pada form tambahEvent
     * untuk kemudian dimasukkan sebagai nilai untuk membuat baris baru pada tabel events
     */
    public String tambahEvent() throws ParseException {
        List<Event> events = new ArrayList<Event>();
        Event event = new Event();
        EventQuery eq = new EventQuery();
        HttpSession session = request.getSession();

        String namaEvent = request.getParameter("namaEvent");
        String deskripsi = request.getParameter("deskripsi");
        String tanggal = request.getParameter("tanggal");
        String tempat = request.getParameter("tempat");
        String ht = request.getParameter("hargaTiket");
        User uname = (User) session.getAttribute("uname");
        if (namaEvent != null) {
            event.setIdUser(uname);
            event.setNamaEvent(namaEvent);
            event.setDeskripsi(deskripsi);
            event.setTanggal(tanggal);
            event.setTempat(tempat);
            session.setAttribute("tempAddEvent", event);
        } else {
            session.setAttribute("error", "masih ada field kosong");
            return "tambahEvent.jsp";
        }
        if (ht.equals("")) {
            session.setAttribute("error", "masih ada field kosong");
            return "tambahEvent.jsp";
        } else {
            try {
                int hargaTiket = Integer.parseInt(ht);
                event.setHargaTiket(hargaTiket);
            } catch (NumberFormatException nf) {
                session.setAttribute("error", "format harga tiket harus angka");
                return "tambahEvent.jsp";
            }
        }
        String rekening = request.getParameter("rekening");
        String teleponCp = request.getParameter("teleponCp");
        event.setRekening(rekening);
        event.setTeleponCP(teleponCp);
        session.setAttribute("tempAddEvent", event);

        if (namaEvent.equals("") || deskripsi.equals("") || tanggal.equals("")
                || tempat.equals("") || rekening.equals("") || teleponCp.equals("")) {
            session.setAttribute("error", "masih ada field kosong");
            return "tambahEvent.jsp";
        } else {
            try {
                int rek = Integer.parseInt(rekening);
            } catch (NumberFormatException n) {
                session.setAttribute("error", "format rekening harus angka");
                return "tambahEvent.jsp";
            }
            try {
                int telp = Integer.parseInt(teleponCp);
            } catch (NumberFormatException nfe) {
                session.setAttribute("error", "format Contact Person harus angka");
                return "tambahEvent.jsp";
            }
            try {
                DateFormat formatter;
                Date date;
                formatter = new SimpleDateFormat("dd-MM-yyyy");
                date = (Date) formatter.parse(tanggal);
            } catch (ParseException e) {
                session.setAttribute("error", "tanggal salah, gunakan datepicker");
                return "tambahEvent.jsp";
            }
            boolean hasilCheck = eq.checkEvent(namaEvent);
            if (!hasilCheck) {
                eq.createEvent(event);
                session.removeAttribute("tempAddEvent");
                events = eq.getMyEvents(uname);
                request.setAttribute("events", events);
                return "daftarEvent.jsp";
            } else {
                session.setAttribute("error", "nama event sudah terpakai");
                return "tambahEvent.jsp";
            }
        }
    }
    /*method untuk menghapus event berdasarkan id dari promotor bersangkutan
     */

    public String hapusEvent() throws NonexistentEntityException {
        HttpSession session = request.getSession();
        String idEvent = request.getParameter("idEvent");
        List<Event> events = new ArrayList<Event>();
        EventQuery eq = new EventQuery();
        List<Comment> comments = new ArrayList<Comment>();
        CommentQuery cq = new CommentQuery();
        List<HistoryPayment> historyPayments = new ArrayList<HistoryPayment>();
        HistoryPaymentQuery hpq = new HistoryPaymentQuery();

        if (session.getAttribute("uname") != null) {
            User uname = (User) session.getAttribute("uname");
            if (idEvent == null) {
                events = eq.getMyEvents(uname);
                request.setAttribute("events", events);
            } else {
                int id = Integer.parseInt(idEvent);
                Event temp = eq.getEvent(id);
                comments = cq.getComments(temp);
                if (comments != null) {
                    Iterator<Comment> iterator = comments.iterator();
                    while (iterator.hasNext()) {
                        Comment next = iterator.next();
                        cq.deleteComment(next.getIdComment());
                    }
                }
                historyPayments = hpq.getHistoryPayments(temp);
                if (historyPayments != null) {
                    Iterator<HistoryPayment> iterator = historyPayments.iterator();
                    while (iterator.hasNext()) {
                        HistoryPayment next = iterator.next();
                        hpq.deleteHistoryPayment(next.getIdPayment());
                    }
                }
                eq.deleteEvent(id);
                events = eq.getMyEvents(uname);
                request.setAttribute("events", events);
            }
            return "daftarEvent.jsp";
        } else {
            session.setAttribute("error", "sudah logout sebelumnya");
            events = eq.getLimitedEvents();
            request.setAttribute("events", events);
            session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Events</a>");
            return "index.jsp";
        }
    }

    /* method ini berguna untuk meredirect user menuju halaman event
     * dengan syarat session tidak dalam keadaan null
     */
    public String lihatEvent() {
        HttpSession session = request.getSession();
        Event event = new Event();
        List<Event> events = new ArrayList<Event>();
        EventQuery eq = new EventQuery();
        List<Comment> comments = new ArrayList<Comment>();
        Comment comment = new Comment();
        CommentQuery cq = new CommentQuery();
        if (session.getAttribute("uname") != null) {
            String temp = request.getParameter("idEvent");
            int idEvent = Integer.parseInt(temp);
            boolean hasilCheck = eq.checkEvent(idEvent);
            if (hasilCheck) {
                event = eq.getEvent(idEvent);
                session.setAttribute("ename", event);/*ngeliat event non public*/
                Event ename = (Event) session.getAttribute("ename");
                comments = cq.getComments(ename);
                request.setAttribute("comments", comments);
                return "lihatEvent.jsp";
            } else {
                session.setAttribute("error", "nama event tidak ada");
                events = eq.getLimitedEvents();
                request.setAttribute("events", events);
                session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Events</a>");
                return "index.jsp";
            }
        } else {
            String temp = request.getParameter("idEvent");
            int idEvent = Integer.parseInt(temp);
            boolean hasilCheck = eq.checkEvent(idEvent);
            if (hasilCheck) {
                event = eq.getEvent(idEvent);
                session.setAttribute("ename", event);
                Event ename = (Event) session.getAttribute("ename");
                comments = cq.getComments(ename);
                request.setAttribute("comments", comments);
                return "lihatEventPublic.jsp";
            } else {
                session.setAttribute("error", "nama event tidak ada");
                events = eq.getLimitedEvents();
                request.setAttribute("events", events);
                session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Events</a>");
                return "index.jsp";
            }
        }
    }
/*paymentdan comment tidak pake session, yg udah login tidak boleh post 
    /* method untuk meredirect user menuju halaman ubahEvent dengan
     * syarat session tidak dalam keadaan null
     */
    public String ubahEvent() {
        HttpSession session = request.getSession();
        Event event = new Event();
        List<Event> events = new ArrayList<Event>();
        EventQuery eq = new EventQuery();

        if (session.getAttribute("uname") != null) {
            Event ename = (Event) session.getAttribute("ename");
            boolean hasilCheck = eq.checkEvent(ename.getNamaEvent());
            if (hasilCheck) {
                event = ename;
                session.setAttribute("tempEditEvent", event);
                return "editEvent.jsp";
            } else {
                session.setAttribute("error", "nama event tidak ada");
                events = eq.getLimitedEvents();
                request.setAttribute("events", events);
                session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Events</a>");
                return "index.jsp";
            }
        } else {
            session.setAttribute("error", "sudah logout sebelumnya");
            events = eq.getLimitedEvents();
            request.setAttribute("events", events);
            session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Events</a>");
            return "index.jsp";
        }
    }


    /* method untuk menyimpan perubahan data event
     * yaitu mengupdate nilai lama dengan nilai yang baru saja dimasukkan
     * saat mengisi form pada halaman editEvent
     */
    public String simpanUbahEvent() throws ParseException {
        HttpSession session = request.getSession();
        Event event = new Event();
        List<Event> events = new ArrayList<Event>();
        EventQuery eq = new EventQuery();

        if (session.getAttribute("uname") != null) {
            String namaEvent = request.getParameter("namaEvent");
            String deskripsi = request.getParameter("deskripsi");
            String tanggal = request.getParameter("tanggal");
            String tempat = request.getParameter("tempat");
            String ht = request.getParameter("hargaTiket");
            event = (Event) session.getAttribute("ename");
            String tempEname = event.getNamaEvent();
            if (namaEvent != null) {
                event.setNamaEvent(namaEvent);
                event.setDeskripsi(deskripsi);
                event.setTanggal(tanggal);
                event.setTempat(tempat);
                session.setAttribute("tempEditEvent", event);
            } else {
                session.setAttribute("error", "masih ada field kosong");
                return "editEvent.jsp";
            }
            if (ht.equals("")) {
                session.setAttribute("error", "masih ada field kosong");
                return "editEvent.jsp";
            } else {
                try {
                    int hargaTiket = Integer.parseInt(ht);
                    event.setHargaTiket(hargaTiket);
                } catch (NumberFormatException nf) {
                    session.setAttribute("error", "format harga tiket harus angka");
                    return "editEvent.jsp";
                }
            }
            String rekening = request.getParameter("rekening");
            String teleponCp = request.getParameter("teleponCp");
            event.setRekening(rekening);
            event.setTeleponCP(teleponCp);
            session.setAttribute("tempEditEvent", event);

            if (namaEvent.equals("") || deskripsi.equals("") || tanggal.equals("")
                    || tempat.equals("") || ht.equals("")
                    || rekening.equals("") || teleponCp.equals("")) {
                session.setAttribute("error", "masih ada field kosong");
                return "editEvent.jsp";
            } else {
                try {
                    int rek = Integer.parseInt(rekening);
                } catch (NumberFormatException n) {
                    session.setAttribute("error", "format rekening harus angka");
                    return "editEvent.jsp";
                }
                try {
                    int telp = Integer.parseInt(teleponCp);
                } catch (NumberFormatException nfe) {
                    session.setAttribute("error", "format contact person harus angka");
                    return "editEvent.jsp";
                }
                try {
                    DateFormat formatter;
                    Date date;
                    formatter = new SimpleDateFormat("dd-MM-yyyy");
                    date = (Date) formatter.parse(tanggal);
                } catch (ParseException e) {
                    session.setAttribute("error", "tanggal salah, gunakan datepicker");
                    return "editEvent.jsp";
                }
                boolean hasilCheck = eq.checkEvent(namaEvent);
                if (!hasilCheck || namaEvent.equals(tempEname)) {
                    session.setAttribute("ename", event);
                    eq.editEvent(event);
                    session.removeAttribute("tempEditEvent");
                    return "lihatEvent.jsp";
                } else {
                    session.setAttribute("error", "username sudah terpakai");
                    return "editEvent.jsp";
                }
            }
        } else {
            session.setAttribute("error", "sudah logout sebelumnya");
            events = eq.getLimitedEvents();
            request.setAttribute("events", events);
            session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Events</a>");
            return "index.jsp";
        }
    }

    public String searchEvents() {
        List<Event> events = new ArrayList<Event>();
        EventQuery eq = new EventQuery();
        String namaEvent = request.getParameter("namaEvent");
        if (namaEvent != null && !namaEvent.equals("")) {
            events = eq.findEvents(namaEvent);
            request.setAttribute("events", events);
            return "cariEvent.jsp";
        } else {
            return "cariEvent.jsp";
        }
    }
}
